package
{
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.geom.Point;

    public class Level extends Sprite
    {
        private static const COLLISION_DIST : Number = 20.0;
        
        private static const FIRST_BELL_POS : Number = 400.0;
        private static const BELLS_SPACE_Y  : Number = 135.0;
        private static const BORDERS_GAP_X  : Number = 20.0;

        private static const REMOVE_DIST : Number = 600.0; 

        private static const SCORE_STEP : Number = 10;
        
        private var limit_x : Number;
        private var limit_y : Number;
        private var cur_y : Number;
        
        private var currentScoreAdd : Number; 
        
        public function Level()
        {
            super();
        }
        
        public function setDimensions(w : int, h: int) : void
        {
            limit_x = w;
            limit_y = h;
            init();
        }
        
        public function init() : void
        {
            // remove all childs instead of Rabbit
            for(var i : int = 0; i < numChildren; i++)
            {
                if(getChildAt(i) is Rabbit)
                    continue;
                else
                {
                    removeChildAt(i);
                    i = 0;
                }
            }            
            
            currentScoreAdd = SCORE_STEP;
            
            cur_y = FIRST_BELL_POS;
            while(cur_y > -Bell.WIDTH)
            {
                createBell();
                cur_y -= BELLS_SPACE_Y;
            } 
        }
        
        private function createBell() : void
        {
            var bell : Bell = new Bell();
            bell.x = BORDERS_GAP_X + Bell.WIDTH + Math.random() * (limit_x - 2*(BORDERS_GAP_X + Bell.WIDTH));
            bell.y = cur_y;
            addChild(bell);
        }
        
        private function checkCollision(rabbit : Rabbit) : void
        {
            const rabbit_pos : Point = rabbit.localToGlobal(new Point(0, 0));
            for(var i : int = 0; i < numChildren; i++)
            {
                const bell : Bell = getChildAt(i) as Bell;
                if(bell == null)
                    continue;
                const bell_pos : Point = bell.localToGlobal(new Point(0, 0));
                const dist : Number = Point.distance(rabbit_pos, bell_pos);
                if(dist < COLLISION_DIST)
                {
                    rabbit.jump();
                    bell.boom(currentScoreAdd);
                    
                    Globals.score += currentScoreAdd;
                    dispatchEvent(new Event(Globals.EVENT_UPDATE_GAME_SCORE, true));
                    
                    currentScoreAdd += SCORE_STEP;                    
                }
            }
        }
        
        private function checkForRemove() : void
        {
            for(var i : int = numChildren-1; i >= 0; i--)
            {
                const bell : Bell = getChildAt(i) as Bell;
                if(bell == null)
                    continue;
                const bell_pos : Point = bell.localToGlobal(new Point(0, 0));
                if(bell_pos.y > limit_y + REMOVE_DIST)
                    removeChild(bell);
            }
        }
        
        public function tick(rabbit : Rabbit) : void
        {
            checkCollision(rabbit);
            checkForRemove();
            
            var p : Point = localToGlobal(new Point(0,0));
            if(-p.y < cur_y + Bell.HEIGHT)
            {
                createBell();
                cur_y -= BELLS_SPACE_Y;
            }
        }
    }
}